#include <iostream>
#include <queue>
using namespace std;
const int N = 1e6 + 10;
int n;
int l[N],r[N];
queue<int> q;
int dfs(int root)
{
	if(!root) return 0;
	return max(dfs(l[root]),dfs(r[root]))+1;
}
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin >> l[i] >> r[i];
	}
	cout << dfs(1) << endl;
}
